package com.ysmygx.manage;

import com.alibaba.fastjson2.JSON;
import com.ysmygx.config.RequestBodyFactory;
import com.ysmygx.dto.InteractionDto;
import com.ysmygx.entity.*;
import com.ysmygx.service.BankInteraction;
import com.ysmygx.service.EnvelopeService;
import com.ysmygx.service.SignService;
import com.ysmygx.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service
public class BankManage {

    @Autowired
    private SignService signService;

    @Autowired
    private EnvelopeService envelopeService;

    @Autowired
    private BankInteraction bankInteraction;

    @Autowired
    private RequestBodyFactory requestBodyFactory;


    @Value("${pay.query-balance-url}")
    private String queryBalanceUrl;

    @Value("${pay.single-transfer-url}")
    private String singleTransferUrl;

    @Value("${pay.query-order-status-url}")
    private String queryOrderStatusUrl;

    @Value("${pay.query-account-transfer-history-url}")
    private String queryAccountTransHisUrl;


    /**
     * 查询余额
     * @param accountNo
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public BalanceResp getBalance(String accountNo) throws InstantiationException, IllegalAccessException {
        // 定义请求体
        BalanceReq reqBody = new BalanceReq();
        reqBody.setAccountNo(accountNo);
        RequestBody<BalanceReq> requestBody = requestBodyFactory.build(reqBody);
        String requestBodyJson = JSON.toJSONString(requestBody);

        // 获取签名
        String signData = signService.sign(requestBodyJson);

        // 制作加密信封
        String encEnvelope = envelopeService.enc(requestBodyJson);

        // 请求银行查询余额服务
        InteractionDto interactionDto = new InteractionDto();
        interactionDto.setSignData(signData);
        interactionDto.setCiphertext(encEnvelope);
        interactionDto.setUrl(queryBalanceUrl);

        String encResult = bankInteraction.interaction(interactionDto);
        // 解密信封
        BalanceResp balanceData = (BalanceResp) envelopeService.dec(encResult, BalanceResp.class);


        return balanceData;
    }

    /**
     * 单笔转账
     * @param singleTransferReq
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public SingleTransferResp singleTransfer(SingleTransferReq singleTransferReq) throws InstantiationException, IllegalAccessException {
        // 定义请求数据
        RequestBody<SingleTransferReq> requestBody = requestBodyFactory.build(singleTransferReq);
        requestBody.getSysHead().setSeqNo(singleTransferReq.getOrderFlowNo());
        String requestBodyJson = JSON.toJSONString(requestBody);

        // 签名
        String signData = signService.sign(requestBodyJson);

        // 加密信封
        String encEnvelope = envelopeService.enc(requestBodyJson);

        // 转账
        InteractionDto interactionDto = new InteractionDto();
        interactionDto.setSignData(signData);
        interactionDto.setCiphertext(encEnvelope);
        interactionDto.setUrl(singleTransferUrl);
        String encResult = bankInteraction.interaction(interactionDto);

        // 解密信封
        SingleTransferResp singleTransferData = (SingleTransferResp)envelopeService.dec(encResult, SingleTransferResp.class);

        return singleTransferData;
    }

    /**
     * 根据流水号查询交易流水
     * @return
     */
    public OrderStatusResp queryOrderStatus(String orderFlowNo) throws InstantiationException, IllegalAccessException {
        // 定义请求数据
        OrderStatusReq orderStatusReq = new OrderStatusReq(orderFlowNo);
        RequestBody<OrderStatusReq> requestBody = requestBodyFactory.build(orderStatusReq);
        String requestBodyJson = JSON.toJSONString(requestBody);

        // 签名
        String signData = signService.sign(requestBodyJson);

        // 加密信封
        String encEnvelope = envelopeService.enc(requestBodyJson);

        // 转账
        InteractionDto interactionDto = new InteractionDto();
        interactionDto.setSignData(signData);
        interactionDto.setCiphertext(encEnvelope);
        interactionDto.setUrl(queryOrderStatusUrl);
        String encResult = bankInteraction.interaction(interactionDto);

        // 解密信封
        OrderStatusResp orderStatusResp = (OrderStatusResp) envelopeService.dec(encResult, OrderStatusResp.class);

        return orderStatusResp;
    }

    /**
     * 查询账户交易流水
     */
    public AccountTransHisResp queryAccountTransHis(AccountTransHisReq accountTransHisReq) throws InstantiationException, IllegalAccessException {
        // 定义请求数据
        RequestBody<AccountTransHisReq> requestBody = requestBodyFactory.build(accountTransHisReq);
        String requestBodyJson = JSON.toJSONString(requestBody);

        // 签名
        String signData = signService.sign(requestBodyJson);

        // 加密信封
        String encEnvelope = envelopeService.enc(requestBodyJson);

        // 转账
        InteractionDto interactionDto = new InteractionDto();
        interactionDto.setSignData(signData);
        interactionDto.setCiphertext(encEnvelope);
        interactionDto.setUrl(queryAccountTransHisUrl);
        String encResult = bankInteraction.interaction(interactionDto);

        // 解密信封
        AccountTransHisResp accountTransHisResp = (AccountTransHisResp) envelopeService.dec(encResult, AccountTransHisResp.class);

        return accountTransHisResp;
    }

}
